package com.yx.pc.api.controller;

import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.modules.api.annotation.LoginUser;
import com.yx.modules.api.entity.TbUser;
import com.yx.pc.api.Dto.TbCollectionDto;
import com.yx.pc.api.service.PcTdBookService;
import com.yx.pc.api.service.PcTdCollectionService;
import com.yx.pc.api.service.PcTdSectionService;
import com.yx.system.dto.CollectionDTO;
import com.yx.system.entity.TdBook;
import com.yx.system.entity.TdCollection;
import com.yx.system.entity.TdSection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 收藏控制器
 */
@RestController
@RequestMapping("/pc/api/collection")
public class PcTdCollectionController {
    @Autowired
    private PcTdCollectionService tdCollectionService;
    @Autowired
    private PcTdBookService tdBookService;
    @Autowired
    private PcTdSectionService tdSectionService;

    /**
     * 按ID查询收藏
     *
     * @return
     */
    @PostMapping("findById")
    public R findById(int id) {
        return R.ok().put("collection", tdCollectionService.selectByPrimaryKey(id));
    }


    /**
     * 查询所有
     *
     * @param map
     * @return
     */
    @RequestMapping("findByUseruuid")
    public R findAllByCondition(@LoginUser TbUser tbUser,@RequestParam Map<String, Object> map) {
        List<TbCollectionDto> tbCollectionDtos = new ArrayList<>();
        Query query = new Query(map);
        query.put("uid",tbUser.getUuid());
        List<TdCollection> tdCollectionList = tdCollectionService.queryList1(query);
        int total = tdCollectionService.queryTotal1(query);
        for (int i = 0; i < tdCollectionList.size(); i++) {
            TbCollectionDto cd = new TbCollectionDto();
            cd.setTdCollection(tdCollectionList.get(i));
            TdBook tdBook = tdBookService.selectByPrimaryKey(tdCollectionList.get(i).getBookid());
            if (!tdBook.getAuthorCode().equals("0")){
                cd.setAuthorCode(tdBook.getAuthorCode());
            }
            cd.setAuthorName(tdBook.getAuthorName());
            List<TdSection> tdSections = tdSectionService.findByBookid(tdBook.getId());
            if (tdSections.size()!=0){
                TdSection tdSection = tdSections.get(tdSections.size()-1);
                cd.setNewsetSectionDate(tdSection.getUpdatedate());
                cd.setNewsetSectionId(tdSection.getId());
                cd.setNewsetSectionName(tdSection.getSectionname());
                cd.setNewsetSectionType(tdSection.getSectionType());
            }
            tbCollectionDtos.add(cd);
        }
        PageUtils pageUtil = new PageUtils(tbCollectionDtos, total, query.getLimit(), query.getPage());
        return R.ok().put("data", pageUtil);
    }

    /**
     * 新增或修改
     *
     * @return
     */
//    @PostMapping("saveOrUpdate")
//    public R saveOrUpdate(int bookid, int sectionid,@LoginUser TbUser tbuser) {
//        Map<String, Object> map = new HashMap<>();
//        map.put("bookid", bookid);
//        map.put("uid", tbuser.getUuid());
//        List<TdCollection> tdCollectionList = tdCollectionService.isCollection(map);
//        TdCollection tdCollection = new TdCollection();
//        if (tdCollectionList.size() == 0) {
//            TdBook tdBook = tdBookService.selectByPrimaryKey(bookid);
//            if (sectionid != 0) {
//                TdSection tdSection = tdSectionService.selectByPrimaryKey(sectionid);
//                tdCollection.setSectionid(tdSection.getId());
//                tdCollection.setSectionname(tdSection.getSectionname());
//            } else {
//                tdCollection.setSectionid(0);
//                tdCollection.setSectionname("");
//            }
//            tdCollection.setBookid(bookid);
//            tdCollection.setBookname(tdBook.getBookname());
//            tdCollection.setCreatedate(new Date());
//            tdCollection.setPicurl(tdBook.getPicurl());
//            tdCollection.setUid(tbuser.getUuid());
//            tdCollectionService.insert(tdCollection);
//        } else {
//            tdCollection = tdCollectionService.selectByPrimaryKey(tdCollectionList.get(0).getId());
//            TdBook tdBook = tdBookService.selectByPrimaryKey(bookid);
//            if (sectionid != 0) {
//                TdSection tdSection = tdSectionService.selectByPrimaryKey(sectionid);
//                tdCollection.setSectionid(tdSection.getId());
//                tdCollection.setSectionname(tdSection.getSectionname());
//            } else {
//                tdCollection.setSectionid(0);
//                tdCollection.setSectionname("");
//            }
//            tdCollection.setBookid(bookid);
//            tdCollection.setBookname(tdBook.getBookname());
//            tdCollection.setPicurl(tdBook.getPicurl());
//            tdCollection.setUid(tbuser.getUuid());
//            tdCollectionService.updateByPrimaryKey(tdCollection);
//        }
//        return R.ok();
//    }

    /*
    * 加入收藏
    */
    @RequestMapping("addCollection")
    public R addCollection(@LoginUser TbUser tbUser,Integer bookid,Integer sectionid){
        //先找到图书
        TdBook tdBook = tdBookService.selectByPrimaryKey(bookid);
        //根据图书id和用户uuid和已删除状态查询是否以前已收藏过
        TdCollection tdCollection = tdCollectionService.findByBookidAndUserUUIDAnddelete(tbUser.getUuid(),bookid);
        //如果以前收藏过但是已删除，改变收藏状态为存在，并更新章节。
        if (tdCollection != null){
            //如果章节id不为空就更新最新章节信息
            if (sectionid!=null){
                TdSection tdSection = tdSectionService.selectByPrimaryKey(sectionid);
                tdCollection.setSectionid(tdSection.getId());
                tdCollection.setSectionname(tdSection.getSectionname());
            }
            //改变收藏为存在
            tdCollection.setCollectionStatus(1);
            tdCollectionService.updateByPrimaryKey(tdCollection);
        //如果不存在，就查询是否已经收藏了。
        }else {
            //根据图书id和用户uuid和已存在状态查询收藏是否存在
            TdCollection tdCollection1 = tdCollectionService.findByBookidAndUserUUIDAndExist(tbUser.getUuid(),bookid);
            //存在就图书就是已经收藏了
            if (tdCollection1!=null){
                return R.error("图书已经收藏啦");
            //不存在就新建收藏
            }else {
                TdCollection tdCollection2 = new TdCollection();
                TdBook tdBook1 = tdBookService.selectByPrimaryKey(bookid);
                if (tdBook1==null){
                    return R.error("图书不存在");
                }
                tdCollection2.setCollectionStatus(1);
                if (sectionid!=null){
                    TdSection tdSection = tdSectionService.selectByPrimaryKey(sectionid);
                    tdCollection2.setSectionid(tdSection.getId());
                    tdCollection2.setSectionname(tdSection.getSectionname());
                }
                tdCollection2.setBookid(bookid);
                tdCollection2.setCreatedate(new Date());
                tdCollection2.setUid(tbUser.getUuid());
                tdCollection2.setBookname(tdBook1.getBookname());
                tdCollection2.setPicurl(tdBook1.getPicurl());
                tdCollection2.setReadRation(Double.valueOf(0));
                tdCollectionService.insert(tdCollection2);
                //增加图书收藏数
                tdBook.setCollectionCount(tdBook.getCollectionCount()+1);
                tdBookService.updateByPrimaryKey(tdBook);
            }
        }
        return R.ok();
    }


    /*
    *更新收藏(主要用于点击章节是触发更新最新章节)
    */
    public R updateCollection(Integer sectionid,TdCollection tdCollection){
        TdSection tdSection = tdSectionService.selectByPrimaryKey(sectionid);
        tdCollection.setSectionid(tdSection.getId());
        tdCollection.setSectionname(tdSection.getSectionname());
        tdCollectionService.updateByPrimaryKey(tdCollection);
        return R.ok();
    }

    /**
     * 批量删除 (软删除)
     *
     * @return
     */
    @RequestMapping("delete")
    public R delete(Integer ids[]) {
        tdCollectionService.deleteByPrimaryKey(ids);
        return R.ok();
    }

//    /**
//     * 按书籍ID 和用户查询是否收藏
//     *
//     * @return
//     */
//    @PostMapping("isCollection")
//    public R isCollection(int bookid, @LoginUser TbUser tbUser) {
//        Map<String, Object> map = new HashMap<>();
//        map.put("bookid", bookid);
//        map.put("uid", tbUser.getUuid());
//        List<TdCollection> tdCollectionList = tdCollectionService.isCollection(map);
//        return R.ok().put("isCollection", tdCollectionList.size() > 0 ? tdCollectionList.get(0).getId() : false);
//    }


}
